package com.zhongyu.controller;

import lombok.SneakyThrows;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import org.apache.ibatis.io.Resources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

@Controller

@RequestMapping("/caigou/cgddcx")
public class CaiDaoController {
    @Resource
    private DataSource dataSource;

    /*采购查询*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportExcelCha")
    public void exportEcelCha(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "cai";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportPdfCha")
    public void exportPdfCha(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        System.out.println("输出来自页面携带的参数：" + creatorName);
        System.out.println("输出来自页面携带的参数：" + checkerName);
        System.out.println("输出来自页面携带的参数：" + purchaserName);
        System.out.println("输出来自页面携带的参数：" + stockKeeperName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);
        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="cai";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }






    /*采购审核*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportExcelShen")
    public void exportEcelShen(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
/*        parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);*/
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "shen";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportPdfShen")
    public void exportPdfShen(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        System.out.println("输出来自页面携带的参数：" + creatorName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
      /*  parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);*/
        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="shen";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }







    /*采购确认*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportExcelQue")
    public void exportEcelQue(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
     /*    parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);*/
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "que";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportPdfQue")
    public void exportPdfQue(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
        /* parameters.put("puName", purchaserName);
        parameters.put("stName", stockKeeperName);*/
        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="que";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }









    /*采购确认*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportExcelRu")
    public void exportEcelRu(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        /*     parameters.put("stName", stockKeeperName);*/
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "ru";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportPdfRu")
    public void exportPdfRu(HttpServletResponse response, String id,String providerIdName,String creatorName,String checkerName,String purchaserName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/caiCha.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("chName", checkerName);
        parameters.put("puName", purchaserName);
        /*parameters.put("stName", stockKeeperName);*/
        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="ru";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }
    }

